package com.zizo.autohome.reply.crawl;

import java.sql.Connection;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.zizo.autohome.reply.bean.AutoHomeReply;
import com.zizo.autohome.reply.dao.ReplyDaoOfAutoHome;
import com.zizo.core.Crawl;
import com.zizo.core.DAO;
import com.zizo.core.Errors;
import com.zizo.util.ConnectionPool;
import com.zizo.util.DocUtil;
import com.zizo.util.Util;

public class ReplyCarwlOfAutoHome extends Crawl {
	ReplyDaoOfAutoHome replydao = (ReplyDaoOfAutoHome) dao;

	/**
	 * @Title:ReplyCarwlOfAutoHome
	 * @Description:
	 * @param id
	 * @param dao
	 */
	public ReplyCarwlOfAutoHome(String id, DAO dao) {
		super(id, dao);
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getPages
	 * 
	 * @Description:
	 * 
	 * @param doc
	 * 
	 * @param selectStr
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.CrawlInterface#getPages(org.jsoup.nodes.Document,
	 * java.lang.String)
	 */
	@Override
	public int getPages(Document doc, String selectStr) {
		return 0;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getItems
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#getItems(java.lang.String)
	 */
	@Override
	public void getItems(String url) {
		// url=http://i.service.autohome.com.cn/clubapp/OtherReply-21697561-1.html;
		Document doc = DocUtil.getDoc(url);
		if (doc == null || doc.select("h1").text().contains("Server Error in '/clubapp' Application.")) {
			Errors error = new Errors(3, 3, url);
			replydao.insertError(error);
			return;
		}
		Elements spans = doc.select("span.fcolor_6");
		int pages = 1;
		if (spans.size() > 0) {
			if (Integer.parseInt(spans.first().text()) > 20) {
				Elements pagesItems = doc.select("div.paging a");
				pages = Integer.parseInt(pagesItems.get(pagesItems.size() - 2).text());
			}
		} else {
			return;
		}
		int count = 0;
		for (int i = 1; i < pages + 1; i++) {
			String link = "http://i.service.autohome.com.cn/clubapp/OtherReply-" + this.id + "-" + i + ".html";
			Document docOther = null;
			if (i == 1) {
				docOther = doc;
			} else {
				docOther = DocUtil.getDoc(link);
				if (docOther == null) {
					Errors error = new Errors(3, 3, link);
					replydao.insertError(error);
					continue;
				}
			}
			Elements items = docOther.select("div.m_t15 tr:not(.kill)");
			for (Element item : items) {
				if (item.select("th").size() > 0) {
					continue;
				}
				int hf_yhid = Integer.parseInt(this.id);// 用户ID
				int hf_clubid = 0;// 车论坛id 477
				int hf_bbsid = 0;// 帖子id 51277834
				String hf_bt = "";// 回复标题
				String hf_nr = "";// 回复内容
				int hf_hfuid = 0;// 被回复的uid
				int hf_lc = 0;// 楼层
				String hf_time = "";// 回复时间
				Elements topicTitle = item.select("div.topicTitle div.rcon");
				Element firstEle = topicTitle.get(0);
				Element firstCon = firstEle.select("span.rUser_rc a").first();
				hf_bt = Util.removeFullWidth(firstCon.text());
				String bbsurl = firstCon.attr("href");
				hf_clubid = Integer.parseInt(bbsurl.split("-")[2]);
				hf_bbsid = Integer.parseInt(bbsurl.split("-")[3]);
				Element secondEle = topicTitle.get(1);
				Element secondCon = secondEle.select("span.rUser_rc a").first();
				hf_hfuid = Integer.parseInt(secondCon.attr("href"));
				Element thirdEle = topicTitle.get(2);
				Element thirdCon = thirdEle.select("span.rUser_rc a").first();
				hf_nr = Util.removeFullWidth(thirdCon.text());
				hf_lc = Integer.parseInt(thirdCon.attr("href").split("#")[1]);
				Element time = item.select("td.txtCen").last();
				hf_time = time.text().replaceAll("\n", " ");
				AutoHomeReply reply = new AutoHomeReply(hf_yhid, hf_clubid, hf_bbsid, hf_bt, hf_nr, hf_hfuid, hf_lc,
						hf_time);
				replydao.insertData(reply);
				count++;
			}
		}
		System.out.println(this.id + "\t" + count);
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: crawlContent
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#crawlContent(java.lang.String)
	 */
	@Override
	public void crawlContent(String url) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: run
	 * 
	 * @Description:
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		// this.getItems("http://i.service.autohome.com.cn/clubapp/OtherReply-"
		// + this.id + "-1.html");
		this.crawlErrors(this.id);
	}

	/**
	 * @Title: crawlErrors
	 * @Description: 第二次爬，将有异常的网址重新爬一次
	 * @param url
	 * @return: void
	 * @author: Alexis
	 */
	public void crawlErrors(String url) {
		Document docOther = null;
		docOther = DocUtil.getDoc(url);
		if (docOther == null) {
			Errors error = new Errors(3, 3, url);
			replydao.insertError(error);
			return;
		}
		Elements items = docOther.select("div.m_t15 tr:not(.kill)");
		for (Element item : items) {
			if (item.select("th").size() > 0) {
				continue;
			}
			int hf_yhid = Integer.parseInt(this.id.split("-")[1]);// 用户ID
			int hf_clubid = 0;// 车论坛id 477
			int hf_bbsid = 0;// 帖子id 51277834
			String hf_bt = "";// 回复标题
			String hf_nr = "";// 回复内容
			int hf_hfuid = 0;// 被回复的uid
			int hf_lc = 0;// 楼层
			String hf_time = "";// 回复时间
			Elements topicTitle = item.select("div.topicTitle div.rcon");
			Element firstEle = topicTitle.get(0);
			Element firstCon = firstEle.select("span.rUser_rc a").first();
			hf_bt = Util.removeFullWidth(firstCon.text());
			String bbsurl = firstCon.attr("href");
			hf_clubid = Integer.parseInt(bbsurl.split("-")[2]);
			hf_bbsid = Integer.parseInt(bbsurl.split("-")[3]);
			Element secondEle = topicTitle.get(1);
			Element secondCon = secondEle.select("span.rUser_rc a").first();
			hf_hfuid = Integer.parseInt(secondCon.attr("href"));
			Element thirdEle = topicTitle.get(2);
			Element thirdCon = thirdEle.select("span.rUser_rc a").first();
			hf_nr = Util.removeFullWidth(thirdCon.text());
			hf_lc = Integer.parseInt(thirdCon.attr("href").split("#")[1]);
			Element time = item.select("td.txtCen").last();
			hf_time = time.text().replaceAll("\n", " ");
			AutoHomeReply reply = new AutoHomeReply(hf_yhid, hf_clubid, hf_bbsid, hf_bt, hf_nr, hf_hfuid, hf_lc,
					hf_time);
			replydao.insertData(reply);
			System.out.println(url);
		}
	}

	public static void main(String[] args) {
		Connection currConn = ConnectionPool.getConnection("autohome", "carvoice.tpddns.cn", "3308");
		Connection coreConn = ConnectionPool.getConnection("autohome", "carvoice.tpddns.cn");
		ReplyDaoOfAutoHome dao = new ReplyDaoOfAutoHome(currConn, coreConn);
		ReplyCarwlOfAutoHome reply = new ReplyCarwlOfAutoHome("29262078", dao);
		reply.run();
	}
}
